/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Player.java
 *
 * Created on 17 avr. 2012, 17:32:37
 */
package debate.gui;

import debate.game.Player;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.Toolkit;
import java.awt.Dimension;
import javax.swing.Timer;

/**
 *
 * @author Propriétaire
 */
public class PlayerPane extends javax.swing.JPanel {

    Player p;
    Timer t;

    /** Creates new form Player */
    public PlayerPane() {
        initComponents();
        jListFormula.setModel(new DefaultListModel());
        jListArguments.setModel(new DefaultListModel());
        jListRequests.setModel(new DefaultListModel());
        jProgressBar1.setMaximum(250);
        t = new Timer(1000, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                p.setSpeechtime(p.getSpeechtime() - 1);
                chronojLabel.setText("" + p.getSpeechtime());
                jProgressBar1.setValue(250 - p.getSpeechtime());
            }
        });

    }

    public Player getPlayer() {
        return p;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jListFormula = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListArguments = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        jListRequests = new javax.swing.JList();
        chronojLabel = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        takeHandjButton = new javax.swing.JButton();
        jProgressBar1 = new javax.swing.JProgressBar();
        movesPaneNew1 = new debate.gui.MovesPaneNew();

        setBorder(javax.swing.BorderFactory.createTitledBorder("PlayerName"));
        setPreferredSize(new java.awt.Dimension(320, 475));

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jListFormula.setBorder(javax.swing.BorderFactory.createTitledBorder("Formulas"));
        jListFormula.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jListFormula.setName("jListFormula"); // NOI18N
        jScrollPane1.setViewportView(jListFormula);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jListArguments.setBorder(javax.swing.BorderFactory.createTitledBorder("Arguments"));
        jListArguments.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jListArguments.setName("jListArguments"); // NOI18N
        jScrollPane2.setViewportView(jListArguments);

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        jListRequests.setBorder(javax.swing.BorderFactory.createTitledBorder("Requests"));
        jListRequests.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jListRequests.setName("jListRequests"); // NOI18N
        jScrollPane3.setViewportView(jListRequests);

        chronojLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/chrono.png"))); // NOI18N
        chronojLabel.setText("000");
        chronojLabel.setToolTipText("Speech time remaining");
        chronojLabel.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        chronojLabel.setName("chronojLabel"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        takeHandjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/TakeHandStart.png"))); // NOI18N
        takeHandjButton.setText("Take Hand");
        takeHandjButton.setName("takeHandjButton"); // NOI18N
        takeHandjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                takeHandjButtonActionPerformed(evt);
            }
        });

        jProgressBar1.setName("jProgressBar1"); // NOI18N

        movesPaneNew1.setName("movesPaneNew1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane2)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(movesPaneNew1, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(57, 57, 57)
                                .addComponent(chronojLabel))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(154, 154, 154)
                        .addComponent(takeHandjButton, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(chronojLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(takeHandjButton)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(movesPaneNew1, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jScrollPane1, jScrollPane3});

    }// </editor-fold>//GEN-END:initComponents

    public void newPlayer(String n) {
        p = new Player(n, 250);
        setBorder(javax.swing.BorderFactory.createTitledBorder("" + p));
        chronojLabel.setText("250");
        takeHandjButton.setEnabled(false);
    }

    public void updateDisplay(boolean hasHand, boolean anyoneHasHand) {
        if (hasHand) {
            takeHandjButton.setEnabled(true);
            takeHandjButton.setText("End Turn");
            takeHandjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/TakeHandStop.png"))); // NOI18N
            setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(1, 217, 42), 2), "" + p));

        } else {
            if (anyoneHasHand) {
                takeHandjButton.setEnabled(false);
                takeHandjButton.setText("Waiting...");
            } else {
                takeHandjButton.setEnabled(true);
                takeHandjButton.setText("Take End");
                takeHandjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/TakeHandStart.png"))); // NOI18N
            }
            setBorder(javax.swing.BorderFactory.createTitledBorder("" + p));
        }
        movesPaneNew1.enableMovesButton(hasHand);
    }

    private void takeHandjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_takeHandjButtonActionPerformed
    }//GEN-LAST:event_takeHandjButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel chronojLabel;
    private javax.swing.JList jListArguments;
    private javax.swing.JList jListFormula;
    private javax.swing.JList jListRequests;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private debate.gui.MovesPaneNew movesPaneNew1;
    private javax.swing.JButton takeHandjButton;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the jList1
     */
    public javax.swing.JList getjList1() {
        return jListFormula;
    }

    /**
     * @return the jList2
     */
    public javax.swing.JList getjList2() {
        return jListArguments;
    }

    /**
     * @return the jList3
     */
    public javax.swing.JList getjList3() {
        return jListRequests;
    }

    /**
     * @return the takeHandjButton
     */
    public JButton getTakeHandjButton() {
        return takeHandjButton;
    }

    public Timer getTimer() {
        return t;
    }

    public JButton getAssertButton() {
        return movesPaneNew1.getAssertButton() ;
    }

    public void refreshModel() {
        ((DefaultListModel)jListFormula.getModel()).clear();
        for (String s : p.getFormulaList()) {
                  ((DefaultListModel)jListFormula.getModel()).addElement(s);  
        }
        ((DefaultListModel)jListRequests.getModel()).clear();
        for (String s : p.getRequestList()) {
            ((DefaultListModel)jListRequests.getModel()).addElement(s); 
        }
    }
}
